package com.ydzt.fanka.server.utils;

public class CheakUtil {
	public static boolean checkTac(String cardid,byte[] tac,int iType,byte[] pack){
		boolean flag = false;
		//卡有10长 取后8位
		@SuppressWarnings("unused")
		String cardid_b8 = cardid.substring(2, 10);
		//命令类型
		byte[] cmdType = new byte[1];
		cmdType[0] = (byte) 0xB0;
		//命令
		byte[] cmd = new byte[1];
		cmd[0] = (byte) 0x84;
		//用户保留字
		byte[] standBy = new byte[8];
		//TAC类型
		byte[] tacType = new byte[1];
		tacType[0] = 0x00;
		//tac次主密匙索引
		byte[] index = new byte[2];
		if(iType == 0){
			index[0] = 0x00;
			index[1] = 0x21;
		}
		if(iType == 1){
			index[0] = 0x00;
			index[1] = 0x16;
		}
		if(iType == 2){
			index[0] = 0x00;
			index[1] = 0x18;
		}
		byte[] times = new byte[1];
		times[0] = 0x01;
		//分散数据 卡号后8位
		@SuppressWarnings("unused")
		byte[] data = new byte[8];
		String zeroStr = null;
		if(cardid.length() != 16){
			StringBuilder sb = new StringBuilder();
			int zeroLen = 16 - cardid.length();
			//生成有zerolen个0的字符串
			sb.append(cardid);
			for(int i = 0;i < zeroLen;i ++){
				sb.append("0");
			}
			zeroStr = sb.toString();
		}
		data = ByteConvert.str2BcdNormal(zeroStr);
		
		//tac初始数据
		byte[] initData = new byte[8];
		byte[] tacValue = tac;
		byte[] tacData = null;
		byte[] len = new byte[2];
		if(iType == 0){
			
		}
		if(iType == 1){
			tacData = getTacPay(pack);
			len = ByteConvert.shortToBytes((short)tacData.length);
		}
		if(iType == 2){
			tacData = getTacRacharge(pack);
			len = ByteConvert.shortToBytes((short)tacData.length);
		}
		
		//组包
		int lengthAll = 36 + tacData.length;
		byte[] pkg = new byte[lengthAll];
		int offset = 0;
		System.arraycopy(cmdType, 0, pkg, offset, 1);
		offset += 1;
		System.arraycopy(cmd, 0, pkg, offset, 1);
		offset += 1;
		System.arraycopy(standBy, 0, pkg, offset, 8);
		offset += 8;
		System.arraycopy(tacType, 0, pkg, offset, 1);
		offset += 1;
		System.arraycopy(index, 0, pkg, offset, 2);
		offset += 2;
		System.arraycopy(times, 0, pkg, offset, 1);
		offset += 1;
		System.arraycopy(initData, 0, pkg, offset, 8);
		offset += 8;
		System.arraycopy(tacValue, 0, pkg, offset, 4);
		offset += 4;
		System.arraycopy(len, 0, pkg, offset, 2);
		offset += 2;
		System.arraycopy(tacData, 0, pkg, offset, tacData.length);
		//向加密机发生tac
		return flag;
	}
	
	
	@SuppressWarnings("unused")
	private static byte[] getTacJY(){
		byte[] tacData = new byte[35];
		return tacData;
	}
	
	private static byte[] getTacPay(byte[] pack){
		byte[] tacData = new byte[34];
		//00 07应用序列号
		byte[] appseqArray = new byte[8];
		int offset = 0;
		System.arraycopy(pack, 8, appseqArray, offset, 8);
		offset += 8;
		//交易金额 08 11
		System.arraycopy(pack, 37, appseqArray, offset, 4);
		offset += 4;
		//交易类型标识 12 12
		System.arraycopy(pack, 0, appseqArray, offset, 1);
		offset += 1;
		//SAM卡编号 13 18
		System.arraycopy(pack, 20, appseqArray, offset, 6);
		offset += 6;
		//终端交易序号 19 22
		System.arraycopy(pack, 57, appseqArray, offset, 4);
		offset += 4;
		//交易日期时间BCD 23 29
		System.arraycopy(pack, 26, appseqArray, offset, 7);
		offset += 7;
		//TAC 30 33
		System.arraycopy(pack, 76, appseqArray, offset, 4);
		return tacData;
	}
	
	private static byte[] getTacRacharge(byte[] pack){
		byte[] tacData = new byte[36];
		//00 07应用序列号
		byte[] appseqArray = new byte[8];
		int offset = 0;
		System.arraycopy(pack, 8, appseqArray, offset, 8);
		offset += 8;
		//08 11 交易新金额
		System.arraycopy(pack, 33, appseqArray, offset, 4);
		offset += 4;
		//12 13 电子钱包交易序号
		System.arraycopy(pack, 61, appseqArray, offset, 4);
		offset += 4;
		//14 17 交易金额
		System.arraycopy(pack, 37, appseqArray, offset, 4);
		offset += 4;
		//交易类型标识 18 18
		System.arraycopy(pack, 0, appseqArray, offset, 1);
		offset += 1;
		//19 24 SAM卡编号 19 24
		System.arraycopy(pack, 20, appseqArray, offset, 6);
		offset += 6;
		//交易时间BCD 25 31
		System.arraycopy(pack, 26, appseqArray, offset, 7);
		offset += 7;
		//TAC 32 35
		System.arraycopy(pack, 76, appseqArray, offset, 4);
		return tacData;
	}
}
